<!--
    -  Copyright (c) 2018-2025, haohanwork.com All rights reserved.
  -->
<template>
  <div class="execution">
    <basic-container>
      <h2 class="io-goods-detail">
        基础信息
      </h2>
      <template>
        <el-row class="io-goods-detail basic-information">
          <el-col :span="6">
            <div class="grid-content bg-purple"></div>
            仓库：默认
          </el-col>
          <el-col :span="6" v-for="item in lossType" v:key v-if="item.value == basicInformation[0].lossType">
            <div class="grid-content bg-purple-light">类型：{{item.label}}</div>
          </el-col>
          <el-col :span="6">
            <div class="grid-content bg-purple">单号：{{basicInformation[0].afterProductSn}}</div>
          </el-col>
          <el-col :span="6">
            <div class="grid-content bg-purple-light"></div>
            创建日期：{{basicInformation[0].createDate}}
          </el-col>
        </el-row>
      </template>
    </basic-container>
    <basic-container>
      <h2 class="io-goods-detail">
        入库单商品清单
      </h2>
      <template>
        <el-table :data="basicInformation" border :summary-method="getSummaries" show-summary style="width: 100%">
          <el-table-column prop="id" label="序号" type="index" width="100px"></el-table-column>
          <el-table-column prop="prodcutName" label="商品"></el-table-column>
          <el-table-column prop="unit" sortable label="单位"></el-table-column>
          <el-table-column label="数量" prop="resultNumber" align="center">
            <template slot-scope="scope">
              <el-input v-model="scope.row.resultNumber"/>
            </template>
          </el-table-column>
          <el-table-column prop="modelPrice" sortable label="单价"></el-table-column>
        </el-table>
      </template>
      <template>
        <div class="return-btn">
          <el-button type="primary" @click="save">保存</el-button>
          <el-button type="primary" plain @click="close">取消</el-button>
        </div>
      </template>
    </basic-container>
  </div>
</template>
<!-- 采购退货 -->
<script>
  import {
    editLossRecord
  } from "@/api/scm/warehouse/breakage";
  import {mapGetters} from 'vuex'
  import Util from '@/util/pds/utils'
  import {fetchDict} from '@/util/util';
  let cookieUtil = new Util();
  export default {
    name: 'addpurchaseback',
    data() {
      return {
        basicInformation: [],
        lossType:[],
      }
    },
    computed: {
      ...mapGetters(["permissions",'tagWel', 'tagList', 'tag', 'website']),
      tagLen() {
        return this.tagList.length || 0
      }
    },
    created() {
      fetchDict('scm_loss_type', this.lossType);
      this.init();
    },
    methods: {
      init() {
        this.basicInformation.push(this.$route.query.query);
      },
      //保存
      save(){
        let params = this.basicInformation[0];
        editLossRecord(params).then(res => {
          if (res.data.code == 0) {
            this.$message.success("编辑成功");
            let _this = this;
            setTimeout(function () {
              _this.close();
            },2000)
          } else {
            this.$message.error("编辑失败")
          }
        });
      },
      // 数据合计
      getSummaries(param) {
        const {columns, data} = param;
        const sums = [];
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = '合计';
            return;
          } else if (index !== 3) {
            sums[index] = '';
            return;
          }
          const values = data.map(item => Number(item[column.property]));
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
            sums[index] = sums[index].toFixed(2);
          } else {
            sums[index] = 'N/A';
          }
        });
        return sums;
      },
      close(){
        var str_before = location.href.split('#')[1];
        let { tag, key } = this.findTag(str_before);
        this.$store.commit('DEL_TAG', tag);
        this.$router.go(-1);
      },
      findTag(value) {
        let tag, key
        this.tagList.map((item, index) => {
          if (item.value === value) {
            tag = item
            key = index
          }
        })
        return { tag: tag, key: key }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .io-goods-detail {
    margin-bottom: 10px;
  }

  .settle-accounts {
    text-align: right;
  }

  .basic-information {
    margin-top: 20px;
    font-weight: inherit;
  }

  .return-btn {
    height: 100px;
    width: 100%;
    text-align: center;
    line-height: 100px;
  }
</style>
